<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of capitalAction
 *
 * @author wcj 
 */
class capitalAction extends backendAction {

    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('price_log_ext');
        $this->tgroup = [100 => '普通用户', 1 => '总经销商', 2 => '经销商', 3 => '代理商', 4 => '经纪人'];
    }

    public function index() {
        $map = $this->_search();
        $mod = $this->_mod;
        if (!empty($mod)) {
            $result = $this->_list($mod, $map);
            $this->assign('page', $result['page']);
            $list = $result['list'];

            if ($this->_request('export') != '') {
                $list = $mod->where($map)->select();
            } else {
                $list = $result['list'];
            }
            foreach ($list as $key => $val) {
                $list[$key]['tgroup_name'] = get_tgroup($val['tgroup']);
                $list[$key]['action_name'] = get_action($val['action']);
                $list[$key]['tgroup_1_name'] = get_username($val['tgroup_1']);
                $list[$key]['tgroup_2_name'] = get_username($val['tgroup_2']);
                $list[$key]['tgroup_3_name'] = get_username($val['tgroup_3']);
            }

            if ($this->_request('export') != '') {
                // 引入excel类文件，将订单用excel文件导出来
                Vendor('excelClass.excelclass');
                $excel = new excelClass();
                $excel->echocapital('资金流水明细表' . date('YmdHis') . '.xls', $list);
                exit;
            }
//            return print_r($list);
            $this->assign('list', $list);

            $sum = 0;
            $arr = $mod->where($map)->select();
            foreach ($arr as $key => $val) {
                $sum += $val[price];
            }
            $sum =  number_format($sum, 2, '.', '');
            $list1 = D('team')->field('title')->select();
            $arr = i_array_column_ext($list1, 'title');
            $this->assign('topkey', $arr);
            $this->assign('sum', $sum);
            //统计begin
     
            // 充值
            $map['action']= 'order';
            $order['price_order'] =$this->_mod->where($map)->sum('price');
            //var_dump($order['price_order']);exit;
            if($order['price_order'] == ''){
                $order['price_order'] = 0;
            }unset($map['action']);

            //提现
            $map['action']= 'cash';
            $order['price_cash'] =$this->_mod->where($map)->sum('price');
            if($order['price_cash'] == ''){
                $order['price_cash'] = 0;
            }unset($map['action']);

            //充值
            $map['action']= 'recharge';
            $order['price_recharge'] =$this->_mod->where($map)->sum('price');
            if($order['price_recharge'] == ''){
                $order['price_recharge'] = 0;
            }unset($map['action']);

            //退款
            $map['action']= 'order_refund';
            $order['price_order_refund'] =$this->_mod->where($map)->sum('price');
            if($order['price_order_refund'] == ''){
                $order['price_order_refund'] = 0;
            }unset($map['action']);

            //返佣
            $map['action']= 'commission';
            $order['price_commission'] =$this->_mod->where($map)->sum('price');
            if($order['price_commission'] == ''){
                $order['price_commission'] = 0;
            }unset($map['action']);

            //运费
            $map['action']= 'express';
            $order['price_express'] =$this->_mod->where($map)->sum('price');
            if($order['price_express'] == ''){
                $order['price_express'] = 0;
            }unset($map['action']);


            // $map['status']= array('IN',[8,9]);
            // $map['lottery']= 2;
            // $order['success_refund'] =$this->_mod->where($map)->sum('lottery_total');
            // unset($map['status']);
            // unset($map['lottery']);
            // $order['total'] =$this->_mod->where($map)->sum('lottery_total');
             $this->assign('order', $order);

            $this->display();
        }
    }

    protected function _search() {
        $map = array();
        ($uid = $this->_request('uid', 'trim')) && $map['uid'] = $uid;
        ($keyword = $this->_request('keyword', 'trim')) && $map['uid'] = array('IN', $this->_get_uids($keyword, 'username'));
        ($tele = $this->_request('tele', 'trim')) && $map['uid'] = array('IN', $this->_get_uids($tele, 'tele'));
        ($realname = $this->_request('realname', 'trim')) && $map['uid'] = array('IN', $this->_get_uids($realname, 'realname'));
        // ($remark = $this->_request('remark', 'trim')) && $map['uid'] = array('IN',$this->_get_uids($remark,'remark'));
        //7.15
        if ($_GET['action']) {
            $action = $_GET['action'];
            if ($_GET['action'] != -1) {
                $map['action'] = $_GET['action'];
            }

            $this->assign('action_', $action);
        }

        if ($_GET['tgroup']) {
            $tgroup = $_GET['tgroup'];
            $tgroup = ($_GET['tgroup'] != 100) ? $_GET['tgroup'] : 0;
            $map['tgroup'] = $tgroup;
        }

        if ($_GET['topkey']) {
            $topkey = $_GET['topkey'];
            $map['topkey'] = $topkey;
        }
          if ($_GET['stime']) {
            $time_start = $_GET['stime'] . ' 00:00:00';
            $time_end = $_GET['etime'] . ' 23:59:59';
            $map['add_time'] = array(array('egt', $time_start), array('elt', $time_end));
        }
        //7.15
        if ($_GET['status'] == null) {
            $status = -1;
        } else {
            $status = intval($_GET['status']);
        }
        $status >= 0 && $map['status'] = array('eq', $status);
        $type = intval($_GET['type']);
        $type = $type ? array('eq', $type) : array('eq', 1);
        $map['type'] = $type;

        $this->assign('search', array(
            'keyword' => $keyword,
            'tele' => $tele,
            'realname' => $realname,
            'remark' => 123,
            'tgroup' => $_GET['tgroup'],
            'topkey' => $_GET['topkey'],
            'status' => $status,
            'type' => $type[1],
            'stime' => $_GET['stime'],
            'etime' => $_GET['etime'],
        ));


        return $map;
    }

    public function _get_uids($keyword, $field) {
        $where[$field] = array('like', '%' . $keyword . '%');
        $uidarr = D('user')->where($where)->field('id')->select();
        $_idarr = array();
        foreach ($uidarr as $v) {
            $_idarr[] = $v['id'];
        }
        return implode(',', $_idarr);
    }

}
